<?php

class ApplyController extends Controller {
	
	/**
	 *
	 * @return array action filters
	 */
	public function filters() {
		return array ('accessControl' )		// perform access control for CRUD operations
		;
	}
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * 
	 * @return array access control rules
	 */
	public function accessRules() {
		return array (array ('allow', 		// allow all users to perform 'index' and 'view' actions
		'actions' => array ('index', 'view' ), 'users' => array ('*' ) ), array ('allow', 		// allow authenticated user to perform 'create' and
		               // 'update' actions
		'actions' => array ('apply', 'bind', 'verify' ), 'users' => array ('@' ) ), array ('allow', 		// allow admin user to perform 'admin' and 'delete' actions
		'actions' => array ('admin', 'delete' ), 'users' => array ('admin' ) ), array ('deny', 		// deny all users
		'users' => array ('*' ) ) );
	}
	
	public $defaultAction = 'apply';
	
	public function actionApply() {
		//检查是否绑定了 手机号码
		$uid = Yii::app() -> user -> id;
		$EducatorCertificates = new EducatorCertificates ();
		$exist = $EducatorCertificates -> findByAttributes(array('user_id'=>$uid));
		
		if(!$exist){
			$this -> redirect($this -> createUrl('/educators/bind'));
			Yii::app() -> end();
		}

		//检查是否已经填写了 教育家相关信息 
		$model = new Educators ( 'create' );
		$uid = Yii::app ()->user->id;
		if ($model->findByAttributes ( array ('user_id' => $uid ) )) {
			#$this->redirect ( $this->createUrl ( '/educators' ) );
		}
		
		//获取用户 基本资料
		$profile = YumProfile::model ()->findByAttributes ( array('user_id'=>$uid) );
		
		// isset($_POST['Educators']) &&
		if (isset ( $_POST ['YumProfile'] ) && isset ( $_FILES ['Educators'] )) {
			// $model->attributes=$_POST['Educators'];
			$profile->attributes = $_POST ['YumProfile'];
			$profile->update ();
			
			$upload = UploadedFile::getInstance ( $model, 'eduer_cover' );
			$model->attributes = $_POST ['Educators'];
			$model->eduer_cover = $upload;
			
			$model->euder_province = $_POST['Educators']['province'];
			$model->euder_city = $_POST['Educators']['city'];
			$model->euder_area = $_POST['Educators']['area'];
			
			$model->eduer_id = NULL;
			$model->user_id = $uid;
			$model->eduer_likes = 0;
			$model->eduer_subscribes = 0;
			$model->eduer_audio_counts = 0;
			$model->eduer_status = 1;
			$model->dateline = time ();
			
			if ($model->validate ()) {
				$ext = $upload->getExtensionName ();
				$name = md5 ( $upload->getName () . time () );
				$name = $name . '.' . $ext;
				$path = Yii::app ()->getParams ();
				$UploadPath = $path->imgUploadPath;
				$folder = UploadedFile::getFolder ( $UploadPath );
				$savePath = $folder . '/' . $name ;
				$upload->saveAs ($savePath );
				$img_url = $folder . '/' . $name;
				$model->eduer_cover = str_replace ( $UploadPath, '', $img_url );
				
				//七牛教育家头像同步
				Yii::app() -> qiniu -> uploadImg($model -> eduer_cover,$savePath);

			}
			
			if ($model->save ())
				$this->redirect ( array ('/educators' ) );
		}
		
		$this->render ( 'apply', array ('model' => $model, 'profile' => $profile ) );
	
	}
	
	public function actionVerify() {
		if (isset ( $_POST ['cellphone'] )) {
			$sms = Yii::app ()->sms;
			$cellphone = $_POST ['cellphone'];
			$sms->sendCode ( $cellphone );
		} else {
			echo "no cellphone";	
		}
	}
	
	public function actionBind() {
		
		$EducatorCertificates = new EducatorCertificates ();
		$uid = Yii::app() -> user -> id;
		$exist = $EducatorCertificates -> findByAttributes(array('user_id'=>$uid));

		if($exist){
			$this -> redirect($this -> createUrl('/educators/apply'));
			Yii::app() -> end();
		}
		
		
		if (! empty ( $_POST )) {
			// $this -> redirect($this -> createUrl('/educator/apply'));
			
			$code = $_POST ['EducatorCertificates'] ['verify_code'];
			$sms = Yii::app ()->sms;
			if ($sms->verfifyCode ( $code ) || 1) {
				$EducatorCertificates->cellphone = $_POST ['EducatorCertificates'] ['cellphone'];
				$EducatorCertificates->dateline = time ();
				$EducatorCertificates->user_id = Yii::app()->user -> id;
				$EducatorCertificates->edu_id = NULL;
				if ($EducatorCertificates->validate ()) {
					$EducatorCertificates->save ();
					$this->redirect ( $this->createUrl ( '/educators/apply' ) );
				}
			}
		
		}
		
		$this->render ( 'bind', array ('model' => $EducatorCertificates ) );
	
	}

}